package com.wink.admin.utils.thread;

import java.util.concurrent.*;


/**
 * 自定义线程池
 *
 * @author xw
 * @date 2020/11/13 16:35
 */
public final class CustomThreadPoolManager {
    private ExecutorService poolService = new ThreadPoolExecutor(10,
            30,
            30L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(1024 * 10 * 5),
            new NamedThreadFactory("custom"),
            new ThreadPoolExecutor.CallerRunsPolicy());

    private static final class SingletonHandler {
        private static final CustomThreadPoolManager INSTANCE = new CustomThreadPoolManager();
    }

    private CustomThreadPoolManager() {
    }

    public static CustomThreadPoolManager getInstance() {
        return CustomThreadPoolManager.SingletonHandler.INSTANCE;
    }

    /**
     * 执行线程方法
     *
     * @param command 线程对象
     */
    public void runThread(Runnable command) {
        poolService.execute(command);
    }

    /**
     * 执行有结果的线程返回
     */
    public <T> Future<T> runResultThread(Callable<T> command) {
        return poolService.submit(command);
    }

    /**
     * 停止线程池
     */
    public void stopPool() {
        poolService.shutdown();
    }

}